/**
 * Created by 余保林 on 2017/8/9.
 */
import Vue from 'vue'
import Vuex from 'vuex'
import * as types from './mutation-types'
import userApi from '../../api/admin/user'
import menuApi from '../../api/admin/menu'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: {},
    menu: [],
    isShowLoading: true
  },
  getters: {
  },
  mutations: {
    [types.UPDATE_USERINFO] (state, userInfo) {
      state.userInfo = {...userInfo}
    },
    [types.UPDATE_MENU] (state, menu) {
      state.menu = [...menu]
      for (let menuItem of menu) {
        if (location.pathname.indexOf(menuItem.url) > 0) {
          state.currentMenu = menuItem
          break
        }
      }
      state.isShowLoading = false
    }
  },
  actions: {
    getUserInfo ({commit}) {
      userApi.getUserInfo((userInfo) => {
        commit(types.UPDATE_USERINFO, userInfo)
      })
    },
    getMenu ({commit}) {
      menuApi.getMenuTree((menu) => {
        commit(types.UPDATE_MENU, menu)
      })
    }
  }
})
